<?php

ob_start();

include_once "../connect.php";
// รับค่ามาเก็บไว้ในตัวแปร 
$cn_id = $_GET["cn_id"];
$c_id = $_GET["c_id"];
$emp_id = $_GET["emp_id"];
$cn_number = $_GET["cn_number"];
$c_keyboard = $_GET["c_keyboard"];
$c_mouse = $_GET["c_mouse"];
$c_monitor = $_GET["c_monitor"];

if (!empty($cn_id)) {
    $exeOther = "SELECT * FROM comname
                                                                LEFT JOIN depart ON comname.de_id = depart.id
                                                                LEFT JOIN computer ON comname.cn_id = computer.cn_id
                                                                LEFT JOIN comother ON comname.cn_id = comother.cn_id
                                                                LEFT JOIN employee ON comname.emp_id = employee.emp_id
                                                                LEFT JOIN product_detail ON comother.oth_detail = product_detail.pd_id
                                                                LEFT JOIN product ON product_detail.prd_id = product.prd_id                                                             
                                                                WHERE comname.cn_id ='$cn_id'
                                                                AND type_id = '6'
                                            ";
    $rsOther = mysql_query($exeOther);
    $rOther = mysql_fetch_array($rsOther);
    
    $other_detail = $rOther['oth_detail'];
    $other_id = $rOther['oth_id'];
    
    if (!empty($other_detail)) {

        $upProdeOther = "UPDATE product_detail SET pd_status = 'NoUse' WHERE pd_id = '$other_detail'";
        mysql_query($upProdeOther);

        $exeProdeOther = "SELECT * FROM product_detail WHERE pd_id = '$other_detail'";
        $rsProdeOther = mysql_query($exeProdeOther);
        $rProdeOther = mysql_fetch_array($rsProdeOther);
        $cnPrd_id = $rProdeOther['prd_id'];

        $exeProbaOther = "SELECT * FROM product WHERE prd_id = '$cnPrd_id'";
        $rsProbaOther = mysql_query($exeProbaOther);

        $rProbaOther = mysql_fetch_array($rsProbaOther);
        $Prd_balance = $rProbaOther['prd_balance'];
        $upProbaOther = "UPDATE product SET prd_balance = '$Prd_balance' + '1' WHERE prd_id = '$cnPrd_id'  ";

        mysql_query($upProbaOther);

        $deComOther = "DELETE FROM comother WHERE oth_id = $other_id";
        mysql_query($deComOther);
        
    } else {
        echo mysql_error();
    }
}


if (!empty($cn_id)) {
    $exeOther2 = "SELECT * FROM comname
                                                                LEFT JOIN depart ON comname.de_id = depart.id
                                                                LEFT JOIN computer ON comname.cn_id = computer.cn_id
                                                                LEFT JOIN comother ON comname.cn_id = comother.cn_id
                                                                LEFT JOIN employee ON comname.emp_id = employee.emp_id
                                                                LEFT JOIN product_detail ON comother.oth_detail = product_detail.pd_id
                                                                LEFT JOIN product ON product_detail.prd_id = product.prd_id                                                             
                                                                WHERE comname.cn_id ='$cn_id'
                                                                AND type_id = '5'
                                            ";
    $rsOther2 = mysql_query($exeOther2);
    $rOther2 = mysql_fetch_array($rsOther2);
    
    $other_detail2 = $rOther2['oth_detail'];
    $other_id2 = $rOther2['oth_id'];
    
    if (!empty($other_detail2)) {

        $upProdeOther2 = "UPDATE product_detail SET pd_status = 'NoUse' WHERE pd_id = '$other_detail2'";
        mysql_query($upProdeOther2);

        $exeProdeOther2 = "SELECT * FROM product_detail WHERE pd_id = '$other_detail2'";
        $rsProdeOther2 = mysql_query($exeProdeOther2);
        $rProdeOther2 = mysql_fetch_array($rsProdeOther2);
        $cnPrd_id2 = $rProdeOther2['prd_id'];

        $exeProbaOther2 = "SELECT * FROM product WHERE prd_id = '$cnPrd_id2'";
        $rsProbaOther2 = mysql_query($exeProbaOther2);

        $rProbaOther2 = mysql_fetch_array($rsProbaOther2);
        $Prd_balance2= $rProbaOther2['prd_balance'];
        $upProbaOther2 = "UPDATE product SET prd_balance = '$Prd_balance2' + '1' WHERE prd_id = '$cnPrd_id2'  ";

        mysql_query($upProbaOther2);

        $deComOther2 = "DELETE FROM comother WHERE oth_id = $other_id2";
        mysql_query($deComOther2);
        
    } else {
        echo mysql_error();
    }
}



if (!empty($cn_id)) {
    $exeOther3 = "SELECT * FROM comname
                                                                LEFT JOIN depart ON comname.de_id = depart.id
                                                                LEFT JOIN computer ON comname.cn_id = computer.cn_id
                                                                LEFT JOIN comother ON comname.cn_id = comother.cn_id
                                                                LEFT JOIN employee ON comname.emp_id = employee.emp_id
                                                                LEFT JOIN product_detail ON comother.oth_detail = product_detail.pd_id
                                                                LEFT JOIN product ON product_detail.prd_id = product.prd_id                                                             
                                                                WHERE comname.cn_id ='$cn_id'
                                                                AND type_id = '8'
                                            ";
    $rsOther3 = mysql_query($exeOther3);
    $rOther3 = mysql_fetch_array($rsOther3);
    
    $other_detail3 = $rOther3['oth_detail'];
    $other_id3 = $rOther3['oth_id'];
    
    if (!empty($other_detail3)) {

        $upProdeOther3 = "UPDATE product_detail SET pd_status = 'NoUse' WHERE pd_id = '$other_detail3'";
        mysql_query($upProdeOther3);

        $exeProdeOther3 = "SELECT * FROM product_detail WHERE pd_id = '$other_detail3'";
        $rsProdeOther3 = mysql_query($exeProdeOther3);
        $rProdeOther3 = mysql_fetch_array($rsProdeOther3);
        $cnPrd_id3 = $rProdeOther3['prd_id'];

        $exeProbaOther3 = "SELECT * FROM product WHERE prd_id = '$cnPrd_id3'";
        $rsProbaOther3 = mysql_query($exeProbaOther3);

        $rProbaOther3 = mysql_fetch_array($rsProbaOther3);
        $Prd_balance3= $rProbaOther3['prd_balance'];
        $upProbaOther3 = "UPDATE product SET prd_balance = '$Prd_balance3' + '1' WHERE prd_id = '$cnPrd_id3'  ";

        mysql_query($upProbaOther3);

        $deComOther3 = "DELETE FROM comother WHERE oth_id = $other_id3";
        mysql_query($deComOther3);
        
    } else {
        echo mysql_error();
    }
}




if (!empty($cn_id)) {
    $sqlUpdatecnid = "UPDATE employee SET cn_id='' WHERE emp_id = '$emp_id'";
    mysql_query($sqlUpdatecnid);
    $deComnam = "DELETE FROM comname WHERE cn_id = $cn_id";
    mysql_query($deComnam);
    $deCompu = "DELETE FROM computer WHERE c_id = $c_id";
    mysql_query($deCompu);


    if (!empty($cn_number)) {

        $upProdeCN = "UPDATE product_detail SET pd_status = 'NoUse' WHERE pd_id = '$cn_number'";
        mysql_query($upProdeCN);

        $exeProdeCn = "SELECT * FROM product_detail WHERE pd_id = '$cn_number'";
        $rsProdeCn = mysql_query($exeProdeCn);
        $rProdeCn = mysql_fetch_array($rsProdeCn);
        $cnPrd_id = $rProdeCn['prd_id'];

        $exeProbaCN = "SELECT * FROM product WHERE prd_id = '$cnPrd_id'";
        $rsProbaCN = mysql_query($exeProbaCN);

        $rProbaCN = mysql_fetch_array($rsProbaCN);
        $Prd_balance = $rProbaCN['prd_balance'];
        $upProbaCN = "UPDATE product SET prd_balance = '$Prd_balance' + '1' WHERE prd_id = '$cnPrd_id'  ";

        mysql_query($upProbaCN);
    } else {
        echo mysql_error();
    }

    if (!empty($c_keyboard)) {

        $upProdeKey = "UPDATE product_detail SET pd_status = 'NoUse' WHERE pd_id = '$c_keyboard'";
        mysql_query($upProdeKey);

        $exeKeyDe = "SELECT * FROM product_detail WHERE pd_id = '$c_keyboard'";
        $rsKeyDe = mysql_query($exeKeyDe);

        $rKeyDe = mysql_fetch_array($rsKeyDe);
        $Prd_idKey = $rKeyDe['prd_id'];
        $exeKey = "SELECT * FROM product WHERE prd_id = '$Prd_idKey'";
        $rsKey = mysql_query($exeKey);

        $rKey = mysql_fetch_array($rsKey);
        $Prd_balKey = $rKey['prd_balance'];

        $upKey = "UPDATE product SET prd_balance = '$Prd_balKey' + '1' WHERE prd_id = '$Prd_idKey' ";
        mysql_query($upKey);
    } else {
        echo mysql_error();
    }

    if (!empty($c_mouse)) {

        $upProMouse = "UPDATE product_detail SET pd_status = 'NoUse' WHERE pd_id = '$c_mouse'";
        mysql_query($upProMouse);

        $exeMousDe = "SELECT * FROM product_detail WHERE pd_id = '$c_mouse'";
        $rsMouseDe = mysql_query($exeMousDe);
        $rMousDe = mysql_fetch_array($rsMouseDe);
        $prd_idMouse = $rMousDe['prd_id'];
        $exeMouse = "SELECT * FROM product WHERE prd_id = '$prd_idMouse' ";
        $rsMouse = mysql_query($exeMouse);

        $rMouse = mysql_fetch_array($rsMouse);
        $Prd_balanceMou = $rMouse['prd_balance'];

        $upMouseBal = "UPDATE  product SET prd_balance = '$Prd_balanceMou' + '1' WHERE prd_id = '$prd_idMouse'";
        mysql_query($upMouseBal);
    } else {
        echo mysql_error();
    }

    if (!empty($c_monitor)) {

        $upProMoni = "UPDATE product_detail SET pd_status = 'NoUse' WHERE pd_id = '$c_monitor'";
        mysql_query($upProMoni);


        $exeMoniDe = "SELECT * FROM product_detail WHERE pd_id = '$c_monitor'";
        $rsMoniDe = mysql_query($exeMoniDe);

        $rMoniDe = mysql_fetch_array($rsMoniDe);
        $Prd_idMoniDe = $rMoniDe['prd_id'];
        $exeMoni = "SELECT * FROM product WHERE prd_id = '$Prd_idMoniDe'";
        $rsMoni = mysql_query($exeMoni);
        $rMoni = mysql_fetch_array($rsMoni);
        $Prd_balanceMoni = $rMoni['prd_balance'];

        $upMoniBalan = "UPDATE product SET prd_balance = '$Prd_balanceMoni' + '1' WHERE prd_id = '$Prd_idMoniDe'";

        mysql_query($upMoniBalan);
    } else {
        echo mysql_error();
    }

    header("location:listCom.php");
} else {
    echo mysql_error();
}
?>
